<?php
namespace common\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\utils\FileUpload;
use herosphp\utils\FileUtils;
use Workerman\Protocols\Http;
use herosphp\session\Session;
use herosphp\bean\Beans;
use herosphp\core\WebApplication;

/**
 * demo action
 * @package commom\action
 * @author yangjian<yangjian102621@gmail.com>
 */
class CommonBackendAction extends CommonAction {

	public function __construct(){
	
		parent::__construct();
	}

	public function C_start()
	{
        Session::start();
        $loginService = Beans::get("login");
        $app = WebApplication::getInstance();
        $request = $app->getHttpRequest();
        if (!$loginService->hasLogin()) {
            $this->_redirect(url("common/login/login") . "?url=" . urlencode("http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']));
        }
        $_SESSION['login_time'] = time(0);
		parent::C_start();
	}

    protected function privileges($key = null, $ajax = false, $forceReturn = false)
    {
        //判断是否是超级管理员
        //echo "<pre>";print_r($_SESSION['admin']['is_super']);die;
        if (!$_SESSION['admin']['is_super']) {
            //如果不是超级管理员
            $tmp = $_SESSION['admin']['priv'];
            $priv = array();

            foreach ($tmp as $k => $v) {
                $lst = explode('@', $k);
                $priv[strtolower($lst[0])] = 1;
                $priv[strtolower($lst[0] . '@' . $lst[1])] = 1;
                $priv[strtolower($k)] = 1;
            }

            //获取已有的权限
            if ($key == null) {
                $app = WebApplication::getInstance();
                $request = $app->getHttpRequest();
                $m = $request->getMethod();
                $m = str_replace("do_", "", $m);
                if (!in_array(strtolower($request->getModule() . '@' . $request->getAction() . '@' . $m), array_keys($priv))) {

                    if ($forceReturn) {
                        return false;
                    }
                    if ($ajax == true) {
                        AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, '您没有权限进行此操作');
                    }

                    $this->_redirect(url('/common/login/nopriv'));
                }
                $key = strtolower($request->getModule() . '@' . $request->getAction() . '@' . $request->getMethod());
            } else {
                if (!in_array(strtolower($key), $priv)) {
                    if ($forceReturn) {
                        return false;
                    }

                    if ($ajax == true) {
                        AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, '您没有权限进行此操作');
                    }
                    $this->_redirect(url('/common/login/nopriv'));
                }
            }

        }
        return true;

    }

    protected function _redirect($url)
    {
        header("location:$url");
        die;
    }

    public function _log($remark)
    {
        $service = Beans::get('system.systemOperatorLog.service');

        $app = WebApplication::getInstance();
        $request = $app->getHttpRequest();
        $op = $request->getModule() . '@' . $request->getAction() . '@' . $request->getMethod();
        $lop = $request->getModule() . '@' . $request->getAction() . '@' . ltrim($request->getMethod(), 'do_');
        $privService = Beans::get('system.systemAdmin.service');

        $privInfo = $privService->getNode(array('node_key' => $lop));
        if (!$remark) {
            $param = $request->getParameters();
            $param = $op . ":" . json_encode($param, JSON_UNESCAPED_UNICODE);
            $param = htmlspecialchars($param);
            if (strlen($param) >= 255) {
                $param = mb_substr($param, 0, 255, 'utf-8') . '...';
            }
            $remark = $param;
        }
        $privname = $privInfo['name'];
        if ($privname == '编辑' || $privname == '添加' || $privname == '删除') {
            $module = $request->getModule();
            $action = $request->getAction();

            $moduleSetting = L('ACTION_NAME');

            $privname .= str_replace('管理', '', $moduleSetting[$module][$action]);
            $privname = trim($privname, '') . '信息';
        }

        $data = array(
            'operate' => $privname,
            'remark' => $remark,
            'admin_id' => $this->getAdminId(),
            'company_id' => $this->getCompanyId(),
            'addtime' => date("Y-m-d H:i:s"),
            'edittime' => date("Y-m-d H:i:s"),
            'del_status' => 0
        );
        return $service->add($data);

    }
}
